/*
 ============================================================================
 Page	     : 398
 Name        : dynamicArray.c
 Date	     : 20210407
 Author      : Hbue 18060333
 Copyright   : Your copyright notice
 Description : 动态分配数组
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>		/* 为malloc()、free()提供原型 */

int main(void)
{
	int max = 0;

	puts("What is the maximum number of type double entries?");
	if(scanf("%d", &max) != 1)
	{
		puts("Number not correctly entered -- bye.");
		exit(EXIT_FAILURE);
	}

	double *ptd = NULL;

	ptd = (double *)malloc(max * sizeof(double));
	if(ptd == NULL)
	{
		puts("Memory allocation failed. Goodbye.");
		exit(EXIT_FAILURE);
	}

	int i = 0;
	/* ptd现在指向有max个元素的数组 */
	puts("Enter the value (q to quit):");
	while(i < max && scanf("%lf", &ptd[i]) == 1)
	{
		++i;
	}

	int number = 0;

	printf("Here are you %d entries:\n", number = i);
	for(i = 0;i < number;i++)
	{
		printf("%7.2f ", ptd[i]);
		if(i % 7 == 6)
		{
			putchar('\n');
		}
	}
	if(i % 7 != 0)
	{
		putchar('\n');
	}
	puts("Done.");
	free(ptd);
	
	return 0;
}

